package com.quanjiaolian.order.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.quanjiaolian.order.service.OrderService;
import com.quanjiaolian.order.serviceimpl.OrderServiceImpl;
import com.quanjiaolian.tools.ResponseToJson;

@SuppressWarnings("serial")
public class OrderUpdate extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(OrderUpdate.class.getName());
	OrderService os = new OrderServiceImpl();
	/**
	 * 根据订单编号废除订单
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			String order_id = request.getParameter("order_id");
			int i = os.deleteOrderById(order_id);
			if(i>0){
				resultMap.put("result","OK");
			}else{
				resultMap.put("result","Failed");
			}
		}catch (Exception e) {
			resultMap.put("result","Error");
			logger.info("废除订单失败！");
			e.printStackTrace();
		}
		ResponseToJson.responseOutWithJson(response,resultMap);
	}

	/**
	 * 根据订单编号更新订单状态
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			String order_id = request.getParameter("order_id");
			String order_state = request.getParameter("order_state");
			int i = os.updateOrderState(order_id,Integer.parseInt(order_state));
			if(i>0){
				resultMap.put("result","OK");
			}else{
				resultMap.put("result","Failed");
			}
		}catch (Exception e) {
			resultMap.put("result","Error");
			logger.info("订单状态更新失败！");
			e.printStackTrace();
		}
		ResponseToJson.responseOutWithJson(response,resultMap);
	}

}
